﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTimKiemPhieuXuatHang : Form
    {
        private PhieuXuatHangManagement phieuxuathangMgt = new PhieuXuatHangManagement();
        private CTPhieuXuatHangManagement ctphieuxuathangMgt = new CTPhieuXuatHangManagement();
        private ChiNhanhCongTyMgt chinhanhctyMgt = new ChiNhanhCongTyMgt();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }
        public FrmTimKiemPhieuXuatHang()
        {
            InitializeComponent();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;
            int chinhanh = -1;

            if (cboKhoXuat.SelectedItem != null)
                chinhanh = ((ChiNhanhCongTy)cboKhoXuat.SelectedItem).Id;

            ResultObj result = new ResultObj();
            result = phieuxuathangMgt.GetAllPhieuXuatHang();

            List<PhieuXuatHang> lstPhieuXuatHang = (from pxh in (List<PhieuXuatHang>)result.Result
                                                    where pxh.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) &&
                                                        pxh.NgayThang.Date >= dtTuNgay.Date && pxh.NgayThang.Date <= dtDenNgay.Date
                                                        && pxh.IdChiNhanhCongTy == chinhanh &&
                                                        pxh.IsDelete==0
                                                    select pxh).ToList();

            dgvDuyetPhieuXuatHang.AutoGenerateColumns = false;
            dgvDuyetPhieuXuatHang.DataSource = lstPhieuXuatHang;
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            FrmPhieuXuatHang frmPhieuXuatHang = new FrmPhieuXuatHang();
            frmPhieuXuatHang.NhanVien = this.nhanvien;
            this.Hide();
            frmPhieuXuatHang.ShowDialog();
            this.Show();
        }

        private void dgvDuyetPhieuXuatHang_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDuyetPhieuXuatHang.CurrentRow != null)
            {
                PhieuXuatHang phieuxuathang = (PhieuXuatHang)dgvDuyetPhieuXuatHang.CurrentRow.DataBoundItem;
                List<CTPhieuXuatHang> lstCTphieuxuathang = (from ctpxh in GetAllCTPhieuXuatHang()
                                                            where ctpxh.IdPhieuXuatHang == phieuxuathang.Id
                                                            select ctpxh).ToList();

                FrmPhieuXuatHang frmPhieuXuatHang = new FrmPhieuXuatHang();
                frmPhieuXuatHang.NhanVien = this.nhanvien;
                frmPhieuXuatHang.PhieuXuatHang = phieuxuathang;
                frmPhieuXuatHang.ListCTphieuxuathang = lstCTphieuxuathang;
                frmPhieuXuatHang.IsEdited = true;
                frmPhieuXuatHang.isPrinting = false;
                frmPhieuXuatHang.Show();
                btnTimKiem_Click(null, null);
            }
        }

        private void FrmTimKiemPhieuXuatHang_Load(object sender, EventArgs e)
        {
            cboKhoXuat.DataSource = GetAllChiNhanhCongTy();
            cboKhoXuat.DisplayMember = "TenChiNhanh";
            cboKhoXuat.ValueMember = "Id";
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhctyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)resultObj.Result
                                                           where cn.IsDelete==0
                                                           select cn).ToList();

            return listChiNhanhCongTy;
        }

        public List<CTPhieuXuatHang> GetAllCTPhieuXuatHang()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = ctphieuxuathangMgt.GetAllCTPhieuXuatHang();
            List<CTPhieuXuatHang> listCTPhieuXuatHang = (from ct in(List<CTPhieuXuatHang>)resultObj.Result
                                                             where ct.IsDelete==0
                                                             select ct).ToList();
            return listCTPhieuXuatHang;
        }
    }
}
